<html>
<head>
<title>CpcPaChi - Version 0.01</title>
<style type="text/css">
span {
	font-family: 'Courier New';
	font-size: 10pt;
	color: #000000;
}
.SpanClass0 {
	color: #808080;
}
.SpanClass3 {
	color: #008080;
}
.SpanClass5 {
	font-weight: bold;
	color: #0000FF;
}
.SpanClass7 {
	color: #808080;
}
.SpanClass10 {
	font-weight: bold;
	color: #000080;
}
.SpanClass11 {
}
</style>
</head>
<body bgcolor="#FFFFFF">
<h2>Un compilador sencillo paso a paso (1)</h2>
<p align="right">(Volver al <a href="doc.html">&iacute;ndice</a>) </p>
<p>Este primer acercamiento reconocer&aacute; s&oacute;lo la orden CLS, encargada de borrar la pantalla. La orden en lenguaje ensamblador de un Amstrad CPC equivalente es CALL &amp;BB6C, que en c&oacute;digo m&aacute;quina ser&iacute;a la secuencia de bytes CD, 6C, BB (expresados en hexadecimal). </p>
<p>  Para simplificar la prueba de los programas destino creados, el compilador no generar&aacute; ensamblador. Ni siquiera crear&aacute; un fichero ".BIN" de c&oacute;digo m&aacute;quina, sino un fuente en Basic capaz de "generar ese c&oacute;digo m&aacute;quina". As&iacute;, para una &uacute;nica orden CLS, se obtendr&iacute;a el siguiente programa, que lee los bytes en hexadecimal, los coloca a partir de la direcci&oacute;n 40.000 de la memoria y finalmente llama a esa direcci&oacute;n para poner el programa en marcha: 
</p>
<pre> 10 DATA CD,6C,BB: ' CALL &amp;BB6C 
 20 DATA C9: ' RET 
 30 longitud = 3 
 40 MEMORY 39999 
 50 FOR n=40000 TO 40000+longitud 
 60 READ a$:POKE n,VAL("&amp;"+a$) 
 70 NEXT 
 80 CALL 40000 
 
 </pre>

 <p>Con todas estas consideraciones, el primer acercamiento al compilador es muy sencillo:</p>

<ul>
  <li>Un procedimiento que obtiene una orden (s&oacute;lo una) del usuario, desde el teclado. </li>
  <li>Un procedimiento que analiza la orden, y da un mensaje de error si no es CLS. </li>
  <li>Un procedimiento que genera el c&oacute;digo de destino (que por ahora es fijo). <br>
  </li>
</ul>
<br>
El fuente ser&iacute;a as&iacute;: <br>
<br>
<br>
<span>
<!--StartFragment-->
<span class="SpanClass5">program</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass11">cpcPaChi</span><span class="SpanClass10">;</span><span class="SpanClass0"><br/>
<br/>
</span><span class="SpanClass3">(*&nbsp;Un&nbsp;compilador&nbsp;de&nbsp;Pascal&nbsp;chiquitito&nbsp;para&nbsp;CPC<br/>
&nbsp;&nbsp;&nbsp;Por&nbsp;Nacho&nbsp;Cabanes&nbsp;-&nbsp;Version&nbsp;0.01,&nbsp;preliminar<br/>
&nbsp;&nbsp;&nbsp;<br/>
&nbsp;&nbsp;&nbsp;Versiones&nbsp;hasta&nbsp;la&nbsp;fecha:<br/>
&nbsp;&nbsp;&nbsp;<br/>
&nbsp;&nbsp;&nbsp;Num.&nbsp;&nbsp;&nbsp;Fecha&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cambios<br/>
&nbsp;&nbsp;&nbsp;---------------------------------------------------<br/>
&nbsp;&nbsp;&nbsp;0.01&nbsp;&nbsp;21-Mar-2008&nbsp;&nbsp;Preliminar:&nbsp;solo&nbsp;acepta&nbsp;CLS&nbsp;por&nbsp;teclado<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y&nbsp;genera&nbsp;el&nbsp;codigo&nbsp;correspondiente<br/>
*)</span><span class="SpanClass0"><br/>
<br/>
<br/>
</span><span class="SpanClass5">var</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;</span><span class="SpanClass11">ficheroDestino</span><span class="SpanClass10">:</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass11">text</span><span class="SpanClass10">;</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;</span><span class="SpanClass11">orden</span><span class="SpanClass10">:</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass5">string</span><span class="SpanClass10">;</span><span class="SpanClass0"><br/>
<br/>
<br/>
</span><span class="SpanClass3">(*&nbsp;Obtiene&nbsp;una&nbsp;orden&nbsp;del&nbsp;programa&nbsp;fuente.&nbsp;En&nbsp;esta&nbsp;version,&nbsp;solo<br/>
es&nbsp;una&nbsp;unica&nbsp;orden,&nbsp;y&nbsp;solo&nbsp;se&nbsp;admite&nbsp;"CLS"&nbsp;*)</span><span class="SpanClass0"><br/>
</span><span class="SpanClass5">procedure</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass11">obtenerOrden</span><span class="SpanClass10">;</span><span class="SpanClass0"><br/>
</span><span class="SpanClass5">begin</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;</span><span class="SpanClass5">write</span><span class="SpanClass10">(</span><span class="SpanClass7">'Introduzca&nbsp;la&nbsp;orden&nbsp;a&nbsp;traducir:&nbsp;'</span><span class="SpanClass10">);</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;</span><span class="SpanClass11">readln</span><span class="SpanClass10">(</span><span class="SpanClass11">orden</span><span class="SpanClass10">);</span><span class="SpanClass0"><br/>
</span><span class="SpanClass5">end</span><span class="SpanClass10">;</span><span class="SpanClass0"><br/>
<br/>
</span><span class="SpanClass3">(*&nbsp;Analiza&nbsp;la&nbsp;orden&nbsp;que&nbsp;el&nbsp;usuario&nbsp;ha&nbsp;dado,&nbsp;y&nbsp;sale&nbsp;con&nbsp;un&nbsp;mensaje<br/>
de&nbsp;error&nbsp;si&nbsp;es&nbsp;incorrecta&nbsp;*)</span><span class="SpanClass0"><br/>
</span><span class="SpanClass5">procedure</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass11">analizarOrden</span><span class="SpanClass10">;</span><span class="SpanClass0"><br/>
</span><span class="SpanClass5">begin</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;</span><span class="SpanClass5">if</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass11">upcase</span><span class="SpanClass10">(</span><span class="SpanClass11">orden</span><span class="SpanClass10">)</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass10">&lt;&gt;</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass7">'CLS'</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass5">then</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="SpanClass5">begin</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="SpanClass11">writeln</span><span class="SpanClass10">(</span><span class="SpanClass7">'Error:&nbsp;orden&nbsp;no&nbsp;reconocida.&nbsp;Se&nbsp;esperaba:&nbsp;CLS'</span><span class="SpanClass10">);</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="SpanClass11">halt</span><span class="SpanClass10">;</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="SpanClass5">end</span><span class="SpanClass10">;</span><span class="SpanClass0"><br/>
</span><span class="SpanClass5">end</span><span class="SpanClass10">;</span><span class="SpanClass0"><br/>
<br/>
</span><span class="SpanClass3">(*&nbsp;Genera&nbsp;el&nbsp;codigo&nbsp;de&nbsp;destino:&nbsp;codigo&nbsp;maquina&nbsp;de&nbsp;Amstrad&nbsp;CPC<br/>
dentro&nbsp;de&nbsp;un&nbsp;cargador&nbsp;en&nbsp;Basic&nbsp;*)</span><span class="SpanClass0"><br/>
</span><span class="SpanClass5">procedure</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass11">generarCodigo</span><span class="SpanClass10">;</span><span class="SpanClass0"><br/>
</span><span class="SpanClass5">begin</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;</span><span class="SpanClass11">assign</span><span class="SpanClass10">(</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass11">ficheroDestino</span><span class="SpanClass10">,</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass7">'salida.bas'</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass10">);</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;</span><span class="SpanClass11">rewrite</span><span class="SpanClass10">(</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass11">ficheroDestino</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass10">);</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;</span><span class="SpanClass11">writeln</span><span class="SpanClass10">(</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass11">ficheroDestino</span><span class="SpanClass10">,</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass7">'10&nbsp;DATA&nbsp;CD,6C,BB:&nbsp;''&nbsp;CALL&nbsp;&BB6C'</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass10">);</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;</span><span class="SpanClass11">writeln</span><span class="SpanClass10">(</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass11">ficheroDestino</span><span class="SpanClass10">,</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass7">'20&nbsp;DATA&nbsp;C9:&nbsp;''&nbsp;RET'</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass10">);</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;</span><span class="SpanClass11">writeln</span><span class="SpanClass10">(</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass11">ficheroDestino</span><span class="SpanClass10">,</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass7">'30&nbsp;longitud&nbsp;=&nbsp;3'</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass10">);</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;</span><span class="SpanClass11">writeln</span><span class="SpanClass10">(</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass11">ficheroDestino</span><span class="SpanClass10">,</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass7">'40&nbsp;MEMORY&nbsp;39999'</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass10">);</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;</span><span class="SpanClass11">writeln</span><span class="SpanClass10">(</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass11">ficheroDestino</span><span class="SpanClass10">,</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass7">'50&nbsp;FOR&nbsp;n=40000&nbsp;TO&nbsp;40000+longitud'</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass10">);</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;</span><span class="SpanClass11">writeln</span><span class="SpanClass10">(</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass11">ficheroDestino</span><span class="SpanClass10">,</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass7">'60&nbsp;READ&nbsp;a$:POKE&nbsp;n,VAL("&"+a$)'</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass10">);</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;</span><span class="SpanClass11">writeln</span><span class="SpanClass10">(</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass11">ficheroDestino</span><span class="SpanClass10">,</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass7">'70&nbsp;NEXT'</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass10">);</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;</span><span class="SpanClass11">writeln</span><span class="SpanClass10">(</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass11">ficheroDestino</span><span class="SpanClass10">,</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass7">'80&nbsp;CALL&nbsp;40000'</span><span class="SpanClass0">&nbsp;</span><span class="SpanClass10">);</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;</span><span class="SpanClass11">close</span><span class="SpanClass10">(</span><span class="SpanClass11">ficheroDestino</span><span class="SpanClass10">);</span><span class="SpanClass0"><br/>
</span><span class="SpanClass5">end</span><span class="SpanClass10">;</span><span class="SpanClass0"><br/>
<br/>
</span><span class="SpanClass3">(*&nbsp;Cuerpo&nbsp;del&nbsp;programa&nbsp;*)</span><span class="SpanClass0"><br/>
</span><span class="SpanClass5">begin</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;</span><span class="SpanClass11">obtenerOrden</span><span class="SpanClass10">;</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;</span><span class="SpanClass11">analizarOrden</span><span class="SpanClass10">;</span><span class="SpanClass0"><br/>
&nbsp;&nbsp;</span><span class="SpanClass11">generarCodigo</span><span class="SpanClass10">;</span><span class="SpanClass0"><br/>
</span><span class="SpanClass5">end.</span><span class="SpanClass0"><br/>
</span><!--EndFragment-->

</span>
<p align="right">(Volver al <a href="doc.html">&iacute;ndice</a>) </p>
</body>
</html>
